package com.garbuz.svnadmin.data
{
	import actionlib.common.query.from;

	import com.garbuz.svnadmin.dto.RepoResponseTO;
	import com.garbuz.svnadmin.services.RepoService;

	import mx.collections.ArrayCollection;

	public class RepoDataProvider extends DataProviderBase
	{
		[Bindable]
		public var repos:ArrayCollection;

		[Bindable]
		public var permissions:ArrayCollection;

		override public function retrieveData():void
		{
			getBusy();
			getService().getRepos();
		}

		public function createRepo(repoName:String):void
		{
			getBusy();
			getService().createNewRepo(repoName);
		}

		public function setPermissions(repoNames:Array, permissions:Object):void
		{
			getBusy();
			getService().submitPermissions(repoNames, from(permissions).select(Permission.toTO));
		}

		private function onResult(result:RepoResponseTO):void
		{
			var repoArray:Array = from(result.repos).select(Repo.fromTO);
			repoArray.sortOn("name", Array.CASEINSENSITIVE);
			repos = new ArrayCollection(repoArray);

			permissions = new ArrayCollection(from(result.permissions).select(Permission.fromTO));

			getReady();
		}

		protected function getService():RepoService
		{
			return new RepoService(onResult, onServiceError);
		}
	}
}
